import { Effect, ImmerReducer, Reducer, Subscription } from 'umi';
import { reqAllSwiper, reqAddOne } from '../api/bannerReq'
import { message } from 'antd';


interface ModelState {
    bannerList: object;
}

interface ModelsType {
    state: ModelState;
    effects: {
        getBanner: Effect;
        addBanner: Effect;

    };
    reducers: {
        initBanner: ImmerReducer
    }
    subscriptions: {

    }

}

const BannerModel: ModelsType = {

    state: {
        bannerList: [],
    },
    effects: {
        *getBanner({ payload }, { call, put }) {
            const res = yield call(reqAllSwiper)

            // console.log(res);//这是获取到的结果
            yield put({
                type: 'initBanner',
                payload: res.message
            })
        },
        *addBanner({ payload }, { call, put }) {
            const res = yield call(reqAddOne, payload)
            console.log(res, "获取到的结果");//这是获取到增加的结果
            if (res.code === 200) {
                message.success("添加成功")
                const res = yield call(reqAllSwiper)
                yield put({
                    type: 'initBanner',
                    payload: res.message
                })
            }
            // yield put({
            //     type: 'initBanner',
            //     payload: res.message
            // })
        },

    },
    reducers: {
        initBanner(state, action) {
            // console.log(state, action);
            state.bannerList = action.payload
        },

    },
    subscriptions: {
        setup({ history, dispatch }: any) {
            dispatch({
                type: "getBanner"
            })
        }
    }

};

export default BannerModel;
